package com.slz3.business.frame.steward;

import org.springframework.cglib.core.ReflectUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReflectBeanPropertiesSteward {

    protected static void initNullField(Object obj) {
        if (obj != null)
            Arrays.stream(ReflectUtils.getBeanProperties(obj.getClass())).forEach(propertyDescriptor -> {
                try {
//                System.err.println(propertyDescriptor.getPropertyType().getName());
                    Object value = propertyDescriptor.getReadMethod().invoke(obj);
                    if (propertyDescriptor.getPropertyType().getName().equals(String.class.getTypeName())) {
                        propertyDescriptor.getWriteMethod().invoke(obj, value == null ? "" : value);
                    } else if (propertyDescriptor.getPropertyType().getName().equals(List.class.getTypeName())) {
                        propertyDescriptor.getWriteMethod().invoke(obj, value == null ? new ArrayList() : value);
                    } else if (propertyDescriptor.getPropertyType().getName().equals(ArrayList.class.getTypeName())) {
                        propertyDescriptor.getWriteMethod().invoke(obj, value == null ? new ArrayList() : value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            });
    }

}
